package cdu.sl.service.impl;

import cdu.sl.annotation.KeyAction;
import cdu.sl.common.PageResult;
import cdu.sl.mapper.DepartmentDao;
import cdu.sl.dto.DeptQueryPageParamDTO;
import cdu.sl.entity.Department;
import cdu.sl.entity.Employee;
import cdu.sl.enumeration.KeyActionType;
import cdu.sl.mapper.EmployeeDao;
import cdu.sl.service.DepartmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl implements DepartmentService {

    private final DepartmentDao departmentDao;

    private final EmployeeDao employeeDao;

    /**
     * 查询所有部门
     *
     * @return 部门信息列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<Department> queryAll() {
        return departmentDao.findAll();
    }

    /**
     * 新增部门
     *
     * @param dept 新增部门实体
     */
    @Override
    @KeyAction(KeyActionType.DEP_INSERT)
    public void insert(Department dept) {
        departmentDao.save(dept);
    }

    /**
     * 分页条件查询部门数据
     *
     * @param deptQueryPageParamDTO 分页参数
     * @return 分页数据
     */
    @Override
    @Transactional(readOnly = true)
    public PageResult page(DeptQueryPageParamDTO deptQueryPageParamDTO) {
        // 构造Pageable对象
        Pageable pageable = PageRequest.of(deptQueryPageParamDTO.getPage() - 1, deptQueryPageParamDTO.getPageSize());
        // 获取查询结果
        Page<Department> page;
        if (deptQueryPageParamDTO.getName() == null || deptQueryPageParamDTO.getName().isEmpty()) {
            page = departmentDao.findAll(pageable);
        } else {
            page = departmentDao.findByNameContaining(deptQueryPageParamDTO.getName(), pageable);
        }

        // 返回查询结果
        return new PageResult<>(page.getTotalElements(), page.getContent(),
                page.getNumber() + 1, page.getSize(), page.getTotalPages());
    }

    /**
     * 根据id获取部门信息
     *
     * @param id 部门id
     * @return 部门信息
     */
    @Override
    @Transactional(readOnly = true)
    public Department getById(Integer id) {
        Optional<Department> opt = departmentDao.findById(id);
        opt.ifPresent(department -> log.info("查询到部门信息：{}",department));
        return opt.orElse(null);
    }


    /**
     * 修改部门信息
     *
     * @param dept
     */
    @Override
    @KeyAction(KeyActionType.DEP_UPDATE)
    public void update(Department dept) {
        departmentDao.save(dept);
    }

    /**
     * 根据id删除部门
     *
     * @param id 部门id
     */
    @Override
    @KeyAction(KeyActionType.DEP_DELETE)
    @Transactional
    public void deleteById(Integer id) {
        // 删除部门id的同时修改部门下属职员信息
        // 首先查询部门下属职员信息
        List<Employee> employees = employeeDao.findByDepId(id);
        // 有员工的情况下 执行修改操作
        if (employees != null && !employees.isEmpty()) {
            employees.forEach(employee -> {
                employee.setDepId(null);
                employee.setDepName(null);
                employeeDao.save(employee);
            });
        }
        // 清除所有员工与部门的关联关系之后 再删除部门
        departmentDao.deleteById(id);
    }


    /**
     * 根据部门id 获取部门下属员工
     *
     * @param id 部门id
     * @return 部门下属员工列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<Employee> getEmpsByDeptId(Integer id) {
        return employeeDao.findByDepId(id);
    }


}
